﻿using System.Collections.Generic;
using KLS.PPDS.Core.Entity;
using KLS.PPDS.Data.Database.Repository.EntityRepository;
using System.Linq;

namespace KLS.PPDS.ApplicationService.Manager
{ 
   public class ArticleContentManager:RepositoryManager<ArticleContentRepository>
    {
       /// <summary>
       /// delete
       /// </summary>
       /// <param name="articleContent"></param>
       public static void Delete(ArticleContent articleContent) 
       {
           var x = ContentImageManager.GetContentImageByContentId(articleContent.Id);

           if (x != null && x.Count > 0) 
           {
               foreach (var item in x) 
               {
                   ContentImageManager.Delete(item);
               }
           }

           Repository.Delete(articleContent);
           LogManager.LogDbDelete(articleContent);
       }

       /// <summary>
       /// MakeArticleContentsReadForLayout
       /// </summary>
       /// <param name="artId"></param>
       public static void MakeArticleContentsReadForLayout(int artId) 
       {
           var lst = GetArticleContentByArticleId(artId);
           
           if (lst != null && lst.Count > 0) 
           {
               foreach (var item in lst) 
               {
                   if (item.IsReadyForEditor.Equals(false) || item.IsReadyForGraphic.Equals(false)) 
                   {
                       item.IsReadyForEditor = true;
                       item.IsReadyForGraphic = true;
                       Save(item);
                   }
               }
           }
       }

       /// <summary>
       /// ResetArticleContent
       /// </summary>
       /// <param name="artId"></param>
       public static void ResetArticleContent(int artId) 
       {
           var lst = GetArticleContentByArticleId(artId);

           if (lst != null && lst.Count > 0)
           {
               foreach (var item in lst)
               {
                   if (item.IsReadyForEditor.Equals(true) || item.IsReadyForGraphic.Equals(true))
                   {
                       item.IsReadyForEditor = false;
                       item.IsReadyForGraphic = false;
                       Save(item);
                   }
               }
           }
       }

       /// <summary>
       /// save
       /// </summary>
       /// <param name="articleContent"></param>
       public  static void Save(ArticleContent articleContent)
       {
           articleContent.InitAudit();

           if (articleContent.Id > 0) 
           {
               Repository.Update(articleContent);
               LogManager.LogDbUpdate(articleContent);
           }
           else if (articleContent.Id == 0) 
           {
               Repository.SaveAndEvict(articleContent);
               LogManager.LogDbAdd(articleContent);
           }

           
       }

       /// <summary>
       /// GetAllArticleContent
       /// </summary>
       /// <returns></returns>
       public static List<ArticleContent> GetAllArticleContent()
       {
           return Repository.GetAllArticleContent();
       }

       /// <summary>
       /// GetArticleContentById
       /// </summary>
       /// <param name="articleContentId"></param>
       /// <returns></returns>
       public static ArticleContent GetArticleContentById(int articleContentId)
       {

           return Repository.GetArticleContentById(articleContentId);
       }

       /// <summary>
       /// GetArticleContentByArticleId
       /// </summary>
       /// <param name="articleId"></param>
       /// <returns></returns>
       public static List<ArticleContent> GetArticleContentByArticleId(int articleId)
       {
           return Repository.GetArticleContentByArticleId(articleId);
       }

    }
}
